package com.wmx.eureka_client2020.utils;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * 获取本机 MAC 地址
 * 获取本机 IP 地址
 * 获取程序进程的 PID 值
 *
 * @author wangMaoXiong
 * @version 1.0
 * @date 2022/12/7 10:58
 */
public class SystemUtils {
    /**
     * 获取本机 MAC 地址
     *
     * @return ：返回如 E8-6A-64-72-C7-7E
     */
    public static final String getLocalMac() {
        String localMac = null;
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            /**
             * 获取电脑网卡的AMC地址
             * 返回包含硬件地址的 byte 数组；如果地址不存在或不可访问，则返回 null
             * 如果电脑因为网卡被禁用，则这里获取会返回 null
             */
            byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
            if (mac == null) {
                System.out.println("获取网卡 MAC 地址失败，网卡可能被禁用...");
                return null;
            }
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < mac.length; i++) {
                if (i != 0) {
                    stringBuffer.append("-");
                }
                /**
                 * 转换mac的字节数组
                 */
                int temp = mac[i] & 0xff;
                String str = Integer.toHexString(temp);
                if (str.length() == 1) {
                    stringBuffer.append("0" + str);
                } else {
                    stringBuffer.append(str);
                }
            }
            localMac = stringBuffer.toString().toUpperCase();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return localMac;
    }

    /**
     * 获取本机 IP 地址
     *
     * @return ：返回如 10.104.65.195
     */
    public static final String getLocalIp() {
        String ip = "";
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            //获取本机ip
            ip = inetAddress.getHostAddress();
            //获取本机计算机名称
            String hostName = inetAddress.getHostName();
            System.out.println("IP地址：" + ip);
            System.out.println("主机名：" + hostName);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return ip;
    }

    /**
     * 获取程序进程的 PID 值
     *
     * @return ：返回如 59372
     */
    public static final Integer getPid() {
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        /**
         * String getName()：返回表示正在运行的Java虚拟机的名称
         * 1、通常格式为：进程PID@计算机名称
         */
        // 59372@wangMaoXiong
        System.out.println(runtimeMXBean.getName());
        return Integer.valueOf(runtimeMXBean.getName().split("@")[0]).intValue();
    }

        /**
         * 获取系统中的线程信息
         *
         * @return
         */
        public static final ThreadInfo[] getThreadInfos() {
            ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
            // 返回包含堆栈跟踪和同步信息的所有活动线程的线程信息。此方法返回时，返回数组中包含的某些线程可能已终止.
            ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
            return threadInfos;
        }

    public static void main(String[] args) {
        String localMac = getLocalMac();
        System.out.println(localMac);

        String localIp = getLocalIp();
        System.out.println(localIp);

        System.out.println(getPid());

        System.out.println();
        int i = 1;
        ThreadInfo[] threadInfos = getThreadInfos();
        for (ThreadInfo threadInfo : threadInfos) {
            System.out.println((i++) + "：" + threadInfo);
        }
    }
}